﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using GameEngine.SceneGraph;
using GameEngine.Drawables;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

// Here's some useful stuff
// Properties: http://msdn.microsoft.com/en-us/library/x9fsa0sw%28v=VS.100%29.aspx
// Refactoring - Extract Method: http://msdn.microsoft.com/en-us/library/0s21cwxk.aspx
// Using statement - If you have a type that does not have using statement, click on it, and from the dropdown box choose the first option (using...)

namespace Redland2
{
    public class GameScene : Scene
    {
        #region members
            /// <summary>
            /// The grass sprite.
            /// </summary>
            private Sprite _grass;
            private Sprite _cursor;
           

            public List<Entity> _entities;
            public Level _level;
            public TimeSpan _time;
            public Random rand;
            public int intLevel = 1;

        #endregion members

        #region properties
        #endregion properties

        #region constructors
            public GameScene(IServiceProvider graphicsContainer) : base(graphicsContainer)
            {
            }

            protected override void Initalize()
            {
                
                CreateGrass();
                CreateCursor();
                
                _entities = new List<Entity>();

                Player player = new Player(this,0,true);
                player.Health = 50;
                _entities.Add(player);
                _entities.Add(player.Gun);

                
                Enemy enemy = new Enemy(this, 2);
                enemy.Position = new Vector2(100, 100);
                enemy.Active = false;
                _entities.Add(enemy);
                //_entities.Add(enemy.Gun);
                
                MedPack powerup = new MedPack(this,3, new Vector2(500,600));
                SpeedBoots speedBoots = new SpeedBoots(this,4, new Vector2(300,300));
                _entities.Add(powerup);
                _entities.Add(speedBoots);
               // CreateLevel();
                string levelText = ("Content//Levels//Level1.txt");
                _level = new Level(levelText, this);
               
                //---------------------------

                base.Initalize();
            }

            /// <summary>
            /// Instantiates the cursor.
            /// Sets the cursor's layer.
            /// Offsets the cursor to be centered on it's position.
            /// </summary>
            private void CreateCursor()
            {
                _cursor = new Sprite(Content.Load<Texture2D>("cursor"), Vector2.Zero);
                _cursor.Layer = 1.0f;
                _cursor.SetOffset(Anchor.Center);
                Drawables.Add(_cursor);
            }

            /// <summary>
            /// Instantiates the grass sprite and puts it on the bottomost layer of the scene.
            /// </summary>
            private void CreateGrass()
            {
                _grass = new Sprite(Content.Load<Texture2D>("grass"), Vector2.Zero);
                _grass.Layer = 0.0f;
                Drawables.Add(_grass);
            }
            
        #endregion construcors

        #region methods
            protected override void Update(GameTime gameTime)
            {
                _time = gameTime.TotalGameTime;
                _cursor.Position = Input.GetMousePosition();
                CleanUp();

                foreach (Entity _entity in _entities)
                {
                    for(int i= _entity.index+1; i < _entities.Count(); i++)
                    {
                        if(_entity.BoundingBox.Intersects(_entities.ElementAt(i).BoundingBox) &&  !(_entity.type == "bullet" && _entities.ElementAt(i).type == "bullet")) // try to limit bullet spam lag.
                        {
                            _entity._collisions.Add(_entities.ElementAt(i));
                            _entities.ElementAt(i)._collisions.Add(_entity);
                        }
                    }
                }

                if (intLevel > 1)
                    GoToLevel2();

                base.Update(gameTime);
            }

            private void CleanUp()
            {
                for( int i = 0; i < _entities.Count; )
                {

                    if ( _entities.ElementAt(i).offscreen() || !_entities.ElementAt(i).IsAlive() )
                    {
                        Entity temp = _entities.ElementAt(i);
                        _entities.RemoveAt(i);
                       
                        this.Drawables.Remove(temp);
                    }
                    else
                        _entities.ElementAt(i).index = i++; // re-index
                }
            }

        
            public string DebugText()
            {
                string debugstring = "Health: ";
                if (_entities.ElementAt(0).type == "player")
                {
                    Player player = (Player)_entities.ElementAt(0);
                    debugstring += player.Health;
                }
                debugstring += "                                                  Enemy Health:"; 
                if (_entities.ElementAt(2).type == "enemy")
                {
                    Enemy enemy = (Enemy)_entities.ElementAt(2);
                    debugstring += enemy.Health;
                }
                return debugstring;
            }

            public void GoToLevel2()
            {
                intLevel = 0;
                Player player = (Player) _entities.ElementAt(0);
                for (int i = 1; i < _entities.Count; i++ )
                    _entities.ElementAt(i).Kill();

                CleanUp();
                _entities = new List<Entity>();
                player.SwitchWeapon(1);
                player.Health = 100;
                _entities.Add(player);
                _entities.Add(player.Gun);

                Enemy enemy = new Enemy(this, 2);
                enemy.Position = new Vector2(100, 100);
                enemy.Active = true;
                enemy.Gun = new Pistol(this, _entities.Count, enemy, enemy.Position);
                _entities.Add(enemy);

                string levelText = ("Content//Levels//Level2.txt");
                _level = new Level(levelText, this);
                 
            }
        #endregion methods
    }
}
